import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import { resolve } from 'path'

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  const isProd = mode === 'production'
  // 加载环境变量
  const env = loadEnv(mode, process.cwd())
  
  return {
    plugins: [
      uni(),
      {
        name: 'virtual-three-module',
        enforce: 'pre',
        resolveId(id) {
          if (id === 'three') {
            return 'virtual:three';
          }
          return null;
        },
        load(id) {
          if (id === 'virtual:three') {
            return `
              console.log('[virtual-three] 使用全局THREE对象');
              export const Object3D = window.THREE ? window.THREE.Object3D : class {};
              export const Vector3 = window.THREE ? window.THREE.Vector3 : class {};
              export const Quaternion = window.THREE ? window.THREE.Quaternion : class {};
              export const Euler = window.THREE ? window.THREE.Euler : class {};
              export const Matrix4 = window.THREE ? window.THREE.Matrix4 : class {};
              export const Scene = window.THREE ? window.THREE.Scene : class {};
              export const Camera = window.THREE ? window.THREE.Camera : class {};
              export const PerspectiveCamera = window.THREE ? window.THREE.PerspectiveCamera : class {};
              export const WebGLRenderer = window.THREE ? window.THREE.WebGLRenderer : class {};
              export default window.THREE || {};
            `;
          }
          return null;
        }
      },
      // 替换百度地图AK值
      {
        name: 'html-transform',
        transformIndexHtml(html) {
          return html.replace(/<%= VITE_BAIDU_MAP_AK %>/g, env.VITE_BAIDU_MAP_AK || '');
        }
      }
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        'three': 'virtual:three'
      },
      dedupe: ['vue']
    },
    server: {
      port: 8080,
      host: true,
      hmr: {
        overlay: false
      },
      cors: true,
      proxy: {
        '/api': {
          target: process.env.VITE_API_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    build: {
      sourcemap: !isProd,
      target: ['es2015', 'chrome87', 'safari13'],
      cssTarget: 'chrome87',
      minify: 'terser',
      terserOptions: {
        compress: {
          drop_console: isProd,
          drop_debugger: isProd
        }
      },
      rollupOptions: {
        output: {
          manualChunks: {
            'vue-vendor': ['vue', 'pinia', 'vue-i18n'],
            'data-libs': ['axios', 'dayjs'],
            // 修改map-libs，不再包括@bmapgl-plugin/track（已静态引入）
            'map-libs': ['vue-baidu-map-3x']
          }
        }
      }
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: ''
        }
      }
    },
    optimizeDeps: {
      include: ['axios', 'dayjs'],
      exclude: ['three', '@bmapgl-plugin/track'] // 排除@bmapgl-plugin/track，因为已静态引入
    }
  }
}) 